package com.beta.dto.order;

import com.beta.em.order.OrderMode;
import com.beta.em.order.OrderStatus;
import com.beta.em.order.PayMode;
import com.beta.em.order.PayStatus;
import com.beta.support.OrderItemCollector;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by lichao on 2016/11/22.
 */
@Getter
@Setter
public class OrdersDto implements Serializable {

    private static final long serialVersionUID = -592122986137880326L;

    private Integer id;

    private String code;

    private BigDecimal price;

    private Integer userId;

    private String remark;

    private String cityName;

    private String cityCode;

    private String mode;

    private String payMode;

    private String status;

    private String payStatus;

    private String payUrl;

    private String receiverName;

    private String receiverPhone;

    private String receiverAddress;

    private Date createdAt;

    private Date updatedAt;

    private Date canceledAt;

    private Date expiredAt;

    private List<? extends OrderItemDto> orderItems;

    public boolean getPayable() {
        if ("paying".equals(this.status) || "freight_paying".equals(this.status)) {
            return true;
        }
        return false;
    }

    public BigDecimal getPrice() {
        return OrderItemCollector.getPrice(this.getOrderItems());
    }

    public BigDecimal getWeight() {
        return OrderItemCollector.getWeight(this.getOrderItems());
    }

    public BigDecimal getFreight() {
        return OrderItemCollector.getFreight(this.getOrderItems());
    }

    public BigDecimal getAmount() {
        return this.getPrice().add(this.getFreight());
    }

    public Integer getQuantity() {
        return OrderItemCollector.getQuantity(this.getOrderItems());
    }

    public Boolean getCollectable() {
        if (PayMode.COD.getValue().equals(this.payMode) && this.price.compareTo(BigDecimal.ZERO) > 0) {
            return true;
        }
        return false;
    }

    public Boolean getConfirmable() {
        if (OrderStatus.DELIVERING.getValue().equals(this.status)
                && OrderMode.BUYING.getValue().equals(this.mode)
                && PayMode.ONLINE.getValue().equals(payMode)) {
            return true;
        }
        return false;
    }

    public Boolean getCancelable() {
        return OrderStatus.PAYING.getValue().equals(this.status) || OrderStatus.FREIGHT_PAYING.getValue().equals(this.status);
    }

    public String getPayStatusText() {
        return PayStatus.getCnValue(this.payStatus);
    }

    public String getStatusText() {
        return OrderStatus.getCnValue(this.status);
    }

}
